package com.gxa.order.service.impl;

import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.order.common.UploadController;
import com.gxa.order.mapper.HotelMapper;
import com.gxa.order.pojo.Hotel;
import com.gxa.order.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;


    @Override
    public JsonResult findAllHotelByCondition(HashMap map) {
        JsonResult jsonResult = new JsonResult();
        int currentPage = Integer.parseInt(map.get("currentPage").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        currentPage = (currentPage-1)*pageSize;
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        try {
            List result = hotelMapper.findAllHotelByCondition(map);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(result);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @Override
    public R findHotelById(Integer hotel_id) {
        R r = new R();
        try {
            Map map = new HashMap();
            Hotel hotel = hotelMapper.selectHotelById(hotel_id);
            List waiguan = Arrays.asList(hotel.getAppearancePicture().split(","));
            List fangjian = Arrays.asList(hotel.getRoomPicture().split(","));
            List canyin = Arrays.asList(hotel.getRestaurantPicture().split(","));
            List xiuxian = Arrays.asList(hotel.getArderPicture().split(","));
            List gonggong = Arrays.asList(hotel.getPublicAreaPicture().split(","));
            List qita = Arrays.asList(hotel.getOtherPicture().split(","));
            map.put("hotel",hotel);
            map.put("waiguan",waiguan);
            map.put("fangjian",fangjian);
            map.put("canyin",canyin);
            map.put("xiuxian",xiuxian);
            map.put("gonggong",gonggong);
            map.put("qita",qita);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
            HashMap data = (HashMap)r.getData();
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }



    /**
     * 添加酒店和民宿
     * @param hotel 酒店民宿实体类
     * @return
     */
    @Override
    public R addHotel(Hotel hotel) {
        R r = new R();
        //设置时区
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        TimeZone.setDefault(timeZone);

        //获取当前时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        hotel.setCreateTime(format);
        System.out.println(format);
        System.out.println(hotel.getCreateTime());

        try {
            int i = hotelMapper.insert(hotel);
            r.setCode(200);
            r.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，添加失败");
        }
        return r;
    }

    /**
     * 编辑酒店和民宿
     * @param hotel
     * @return
     */
    @Override
    public R updateHotelById(Hotel hotel) {
        R r = new R();
        try {
            int i = hotelMapper.updateById(hotel);
            r.setCode(200);
            r.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，编辑失败");
        }
        return r;
    }

    /**
     * 下架上架
     * @param hotel_id
     * @return
     */
    @Override
    public R delHotelById(Integer hotel_id) {
        R r = new R();
        try {
            Hotel hotel = hotelMapper.selectById(hotel_id);
            if (hotel.getStatus() == 0){
                //使用mybatis-plus的逻辑删除与；
                hotelMapper.deleteHotelById(hotel_id);
                r.setCode(200);
                r.setMsg("下架成功");
            }
            if (hotel.getStatus() == 1){
                int result = hotelMapper.updateHotelStatusById(hotel_id);
                r.setCode(200);
                r.setMsg("上架成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，下架失败");
        }
        return r;
    }

    /**
     * 添加图片
     * @param file
     * @return
     */
    @Override
    public R addImg(MultipartFile file) {
        R r = new R();
        try {
            UploadController uploadController = new UploadController();
            HashMap upload = (HashMap) uploadController.upload(file);
            r.setCode(200);
            r.setMsg("成功");
            r.setData(upload.get("url").toString());
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }


    @Override
    public R agreeExamine(Integer id) {
        R r = new R();
        try{
            int i = hotelMapper.agreeExanmine(id);
            r.setCode(200);
            r.setMsg("审核通过");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("审核失败");
        }
        return r;
    }

    @Override
    public R notAgreeExamine(Integer id) {
        R r = new R();
        try{
            int i = hotelMapper.notAgreeExamine(id);
            r.setCode(200);
            r.setMsg("拒绝退款申请成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    //换房
    @Override
    public R exchangeRoom(HashMap map) {
        R r = new R();
        try {
            //更改住客表的房间号
            int result = hotelMapper.exchangeRoom(map);
            r.setCode(200);
            r.setMsg("换房成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，换房失败");
        }
        return r;
    }

    //添加酒店房间数
    @Override
    public R addRoomNumber(Integer hotel_id, Integer room_number) {
        R r = new R();
        try {
            int result = hotelMapper.addRoomNumber(hotel_id,room_number);
            r.setCode(200);
            r.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，添加失败");
        }

        return r;
    }


}
